package com.gold.zookeeperdemo.lock;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

public class ZkWatcherDemo {
    public static void main(String[] args) {
        //创建一个zk客户端
        ZkClient zkClient=new ZkClient("47.97.23.184:2181");
        zkClient.setZkSerializer(new MyZkSerializer());

        zkClient.subscribeDataChanges("/gold/test", new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                //String s ： path路径；  Object o : 节点的值
                System.out.println("*****节点变化******* s="+s+", 节点值变化为"+o);
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {
                System.out.println("******节点删除通知******, path="+ s +" 已删除");
            }
        });
        //线程睡眠，保持阻塞状态
        try {
            Thread.sleep(1000*60*10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
